package com.hy.example.shared.autoconfigure.swagger;

import com.hy.example.shared.autoconfigure.common.SharedProperties;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringDocConfig {

    @Bean
    public OpenAPI openAPI(SharedProperties sharedProperties) {
        return new OpenAPI()
                .info(this.getApiInfo())
                .addSecurityItem(new SecurityRequirement().addList(sharedProperties.getToken().getName()))
                .components(
                        new Components()
                                .addSecuritySchemes(sharedProperties.getToken().getName(),
                                        new SecurityScheme()
                                                .name(sharedProperties.getToken().getName())
                                                .type(SecurityScheme.Type.APIKEY)
                                                .in(SecurityScheme.In.HEADER)));
    }

    @Bean("defaultGroupApi")
    public GroupedOpenApi defaultGroupApi() {
        return GroupedOpenApi.builder().group("default")
                .pathsToMatch("/rest/**")
                .build();
    }

    @Bean("facadeGroupApi")
    public GroupedOpenApi facadeGroupApi() {
        return GroupedOpenApi.builder().group("facade")
                .pathsToMatch("/facade/**")
                .build();
    }

    private Info getApiInfo() {
        return new Info()
                .title("**系统")
                .description("**系统API文档")
                .contact(new Contact().name("***"))
                .license(new License().name("***"))
                .summary("API文档")
                .version("1.0");
    }

}
